package database;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;

import model.User;
import model.UserInterface;

/**
 * This class handles queries to the database for now and will eventually
 * handle changes and updates to the database in the future once dynamic
 * database compilation and modification is implemented.
 * 
 * @author Tristan D. Boucher 
 * @version Commit #22 9/14/2013
 *
 */
public class DataManager
{
  /**
   * The database that will hold all the users registered in the system
   * and prioritize them by who is used most often.
   */
  private final PriorityBlockingQueue<User> my_user_queue;
  
  /**
   * The flag that will be tripped if the schedule is completed.
   * This allows users to view the entire schedule as well as their 
   * individual schedule. Until then, that view will be unavailable.
   */
  private boolean my_schedule_completed;

  /**
   * The array of all users.
   */
  private Object[] my_users;

  /**
   * Constructor. Loads information and sets variables from database file.
   */
  public DataManager()
  {
    my_schedule_completed = false;

    my_user_queue = new PriorityBlockingQueue<User>();

    my_users = my_user_queue.toArray();
  }

  /**
   * Searches database for the specified user through the username
   * and returns that UserInterface object.
   * 
   * @param the_id The id of the user to find as a string.
   * 
   * @return user The UserInterface object that was found. null if NA.
   */
  public UserInterface findUser(final String the_id)
  {
    final PriorityBlockingQueue<User> temp_queue = new PriorityBlockingQueue<User>();
    temp_queue.addAll(my_user_queue);
    System.out.println(temp_queue.toString());
    System.out.println(the_id);
    User to_return = null;
    while (!temp_queue.isEmpty())
    {
      final User temp_user = temp_queue.poll();
      
      if (!(temp_user == null) &&
          (new String(temp_user.getID())).equals(the_id))
      {
        to_return = temp_user;
      }
    }
    System.out.println(to_return);
    return to_return;
  }

  /**
   * Adds a a User to the list.
   * 
   * @param the_user The user object to add to the queue.
   */
  public void addUser(final User the_user)
  {
    my_user_queue.put(the_user);
    my_users = my_user_queue.toArray();
  }

  /**
   * Loads the Data from the file specified and returns this
   * DataManager instance.
   * 
   * @return This instance of the DataManager class.
   */
  public DataManager loadData()
  {
    //TODO: Create a JFileChooser and finish data loading.
    return this;
  }

  /**
   * Returns true if the schedule is completed.
   * 
   * @return True if the schedule is completed.
   */
  public boolean isScheduleComplete()
  {
    boolean to_return = false;

    if (my_schedule_completed)
    {
      to_return = true;
    }

    return to_return;
  }

  /**
   * Returns true if the user object passed has entered their availability already.
   * 
   * @param the_user The user object that will be queried.
   * @return true if the user object has an availability entry.
   */
  public boolean isAvailabilityEntered(final User the_user)
  {
    boolean to_return = false;

    //TODO: if entered flag then true.

    return to_return;
  }
  
  /**
   * Returns a list of all the tutors.
   * 
   * @return A list of all the users in the system.
   */
  public List<User> requestAllUsers()
  {
    
    final List<User> users = new ArrayList<User>();
    
    for (int i = 0; i < my_users.length; i++)
    {
      users.add((User) my_users[i]);
    }
    
    return users;
  }
}
